<template>
	<view class="auth">
		<image class="image" src="../../static/titlelogo.png" mode=""></image>
		<view class="" v-if="options.Id">
			单号:
			<span>{{options.Id}}</span>
		</view>
		<view class="description">
			申请获取您的手机号,用于货品签收
		</view>
		<div style="display: flex;flex-direction: column;justify-content: space-between;">
			<view class="inputBorder" style="margin-bottom: 30rpx;border:none">
				<uni-easyinput style="border: none;flex:1;font-size:30rpx" type="number" placeholder="请输入手机号"
					v-model="phone"></uni-easyinput>
				<!-- <view v-if="isCode==true"
					style="display: flex;justify-content: space-between;align-items: center;margin-top: 20rpx;">
					<uni-easyinput style="border: none;flex:1;font-size:30rpx" type="number" placeholder="请输入验证码"
						v-model="code"></uni-easyinput>
					<button size="mini" style="height: 38px;line-height: 38px;margin-left:20rpx;margin-top:8rpx"
						type="primary" @click="codeChange">{{!codeTime?'获取验证码':codeTime+'s后重发'}}</button>
				</view> -->
			</view>
			<view style="">
				<button type="primary" style="" @click="getPhoneNumberOne">进入</button>
			</view>
		</div>

		<div class="unit">技术支持 由北京西西技术提供</div>
	</view>
</template>

<script>
	import {
		regPhone
	} from '@/utils/regExp.js'

	// const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	export default {

		data() {
			return {
				gamearr: [],
				options: {},
				userInfo: {},
				flag: false,
				Atoken: '',
				Vtoken: '',
				//授权的手机号
				phone: "",
				itemnumber: {
					item: {},
					index: ''
				},

			}
		},
		async onLoad(options) {
			uni.clearStorageSync()
			this.options = options
			if (options.Id) {
				uni.setStorageSync('Id', options.Id);
			}
			// this.status = false


			// let url = decodeURIComponent(options.q)
			// console.log(url)
			// console.log(url.split('?'), '111111111111111111111111111111');
			// this.query = this.urlParamsToObject(url)
			// console.log(this.urlParamsToObject(url))
			// this.userInfo = uni.getStorageSync('userInfo') || ''
			// console.log(this.userInfo,
			// 	'this.userInfothis.userInfothis.userInfothis.userInfothis.userInfothis.userInfo')
			// if (this.userInfo) {
			// 	this.AuthRealName()
			// }


		},
		onShow() {

		},
		methods: {
			async getAToken() {
				console.log('获取token', this.userInfo)
				await uni.request({
					url: this.$veriUrl + '/auth/VeriRecord/token?phone=' + this.phone,
					method: 'GET'
				}).then(result => {
					let [err, res] = result;
					if (res.data.code == 200) {
						this.Atoken = JSON.parse(res.data.data).access_token
						console.log('获取的token', this.token, this.flag)
					}
				})
			},
			async getVToken() {
				await uni.request({
					url: this.$veriUrl + '/auth/VeriRecord/tokenVK?ak=' +
						this.Atoken,
					method: 'GET',
					// header: {
					// 	'Content-Type': 'application/json'
					// },

				}).then(result => {
					let [err, res] = result;
					if (res.data.code == 200) {
						this.Vtoken = JSON.parse(res.data.data).result.verify_token
					}
				})
			},
			// async getAToken() {
			// 	console.log('获取token', this.userInfo)
			// 	await uni.request({
			// 		url: `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${'DB2oWIwUdI4QfDkRuwdfhAiw'}&client_secret=${'bIzN3rQFdPsPPeXJzq4N61DgqWm0Od6v'}`,
			// 		method: 'POST',

			// 	}).then(result => {
			// 		let [err, res] = result;
			// 		if (res.data.code == 200) {
			// 			this.Atoken = JSON.parse(res.data.data).access_token
			// 			console.log('获取的token', this.token, this.flag)
			// 		}
			// 	})
			// },
			// async getVToken() {
			// 	await uni.request({
			// 		url: 'https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/verifyToken/generate?access_token=' +
			// 			this.Atoken,
			// 		method: 'POST',
			// 		header: {
			// 			'Content-Type': 'application/json'
			// 		},
			// 		data: {
			// 			plan_id: '16384'
			// 		}
			// 	}).then(result => {
			// 		let [err, res] = result;
			// 		if (res.statusCode == 200) {
			// 			this.Vtoken = res.data.result.verify_token
			// 		}
			// 	})
			// },
			async baiduSub(IdCard, UserName) {
				uni.showLoading({
					title: '验证身份中'
				})
				await this.$http('post', '/BO/BillList/IdentityVerification', {
					"id_no": IdCard,
					"id_name": UserName,
					"certificate_type": 0,
					"verify_token": this.Vtoken,

				}).then(res => {
					if (res.Success) {
						if (res.Data.success) {
							let successUrl = encodeURIComponent(
								// `https: //hzq.yoojet.com/#/pass/facePassLoading`
								`https://mashangqian.yoojet.com/#/pages/signFor/signFor?Id=${uni.getStorageSync('Id')}&isType=true&Phone=${this.phone}`
							)
							let errorUrl = encodeURIComponent(
								`https://mashangqian.yoojet.com/#/?Id=${uni.getStorageSync('Id')}`
							)

							let src =
								`https://h5-faceverify.baidu.com/face/print/?token=${this.Vtoken}&successUrl=${successUrl}&failedUrl=${errorUrl}`
							window.open(src, '_self', '', true)
						} else {
							uni.showToast({
								title: '姓名与身份证校验失败',
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: '姓名与身份证校验失败',
							icon: 'none'
						})
					}

				})
			},
			async getverification(UserPhone, UserName, IdCard) {
				uni.showLoading({
					title: '校验签收中,请稍等'
				})
				let flag = false
				await this.$http('post', '/BO/BillList/CheckInfo', {
					"id": uni.getStorageSync('Id') || '',
					"Phone": UserPhone,
					"Name": UserName,
					"Card": IdCard
				}).then(res => {
					if (res.Success) {
						flag = true
						uni.hideLoading()
						uni.showToast({
							title: '校验成功',
							icon: 'none'
						})
					} else {
						flag = false
						uni.showToast({
							title: res.Msg,
							icon: 'none'
						})
					}
				})
				return flag
			},
			async getPhoneNumberOne(e) {

				if (!this.options.Id) {
					uni.showToast({
						title: '请确认单号是否存在',
						icon: "none"
					})
				}
				if (this.phone === "") {
					uni.showToast({
						title: '请先填写手机号',
						icon: "none"
					})
					return
				}
				if (!regPhone.test(this.phone)) {
					uni.showToast({
						title: '手机号格式不正确',
						icon: "none"
					});
					return;
				}

				this.disabled = true
				uni.showLoading({
					title: '校验中,请稍等'
				})
				await this.$http('post', '/BO/BillList/CheckBill', {
					"id": this.options.Id,
					"Phone": this.phone,
				}).then(
					async res => {

						if (res.Success) {
							// 判断公司下是否勾选了签收时是否可修改签收数量   0 不可修改  1  可修改  下歌页面根据该字段判断数量输入框是否可修改条件
							if (res.Data.IsEditNum) {
								uni.setStorageSync('IsEditNum', res.Data.IsEditNum);
							} else {
								uni.setStorageSync('IsEditNum', null);
							}
							//是否有位置验证
							if (res.Data.IsPosition) {
								uni.setStorageSync('IsPosition', res.Data.IsPosition);
								uni.setStorageSync('Distance', res.Data.Distance);
							} else {
								uni.setStorageSync('IsPosition', null);
								uni.setStorageSync('Distance', null);
							}
							if (res.Data.Status != '0') {
								uni.hideLoading()
								return uni.showToast({
									title: '当前单据不能签收',
									icon: "none"
								});
							}
							uni.setStorageSync('showCodeBox', res.Data.SignState);

							//认证方式为微信认证则直接修改状态为已认证
							if (res.Data.SignType == 3) {
								res.Data.IsAuth = true
							}
							if (!res.Data.IsAuth) {
								console.log('是否认证', res.Data.IsAuth)
								uni.hideLoading()
								uni.showToast({
									title: '当前手机号未认证将跳转认证页面',
									icon: "none"
								});
								setTimeout(() => {
									uni.setStorageSync('token', res.Data.Token);
									let userInfo = res.Data.User || {}
									userInfo.UserPhone = userInfo.Phone || this.phone
									userInfo.IdCard = userInfo.IdNumber
									userInfo.UserName = userInfo.Name
									userInfo.userAUrl = userInfo.Avatar
									uni.setStorageSync('userInfo', userInfo);
									uni.setStorageSync('type', res.Data.SignType);
									uni.navigateTo({
										url: `/pages/UpdateInfo/UpdateInfo`
									})
								}, 500)
								return
							}

							if (res.Data.IsAuth) {

								let userInfo = res.Data.User || {}
								userInfo.UserPhone = userInfo.Phone
								userInfo.IdCard = userInfo.IdNumber
								userInfo.UserName = userInfo.Name
								userInfo.userAUrl = userInfo.Avatar

								let flag = await this.getverification(userInfo.Phone, userInfo.Name, userInfo
									.IdNumber)
								if (!flag) {
									return
								}
								uni.setStorageSync('userInfo', userInfo);
								uni.setStorageSync('type', res.Data.SignType);
								uni.setStorageSync('token', res.Data.Token);
								if (res.Data.SignType == '1' && !res.Data.IsAuth) {
									await this.getAToken()
									await this.getVToken()
									uni.hideLoading()
									await this.baiduSub(userInfo.IdCard, userInfo.UserName)
									return
								}
								console.log('可以继续走了', res.Data.IsAuth)
								// 认证方式：0二要素 1人脸识别签收 2 三要素 3微信签收（不需要验证）
								uni.navigateTo({
									url: `/pages/signFor/signFor?Id=${this.options.Id}&Phone=${userInfo.Phone}`
								})

							}
							// let userInfo = res.Data.User
							// await this.AuthRealName(userInfo.cPhone)
							// return

							// if (res.Data.SignTyp == '1') {
							// 	uni.setStorageSync('type', res.Data.SignTyp);

							// }
							// if (res.Data.SignTyp == '0') {
							// 	uni.setStorageSync('type', res.Data.SignTyp);
							// }
							// if (res.Data.SignTyp == '2') {


							// }
							// if (res.Data.SignTyp == '3') {

							// }


						} else {
							uni.hideLoading()
							uni.showToast({
								title: res.Msg,
								icon: "none"
							});
						}
						// if (res.Success) {
						// 	if (res.Data.State) {
						// 		uni.setStorage({
						// 			key: 'token',
						// 			data: res.Data.Token,
						// 		});
						// 		this.userInfo = res.Data.userdata
						// 		uni.setStorage({
						// 			key: 'userInfo',
						// 			data: res.Data.userdata,
						// 			success: () => {
						// 				uni.navigateTo({
						// 					url: `/pages/signFor/signFor?Id=${'1774667297009438720'}`
						// 				})
						// 				// uni.navigateTo({
						// 				// 	url: `/pages/UpdateInfo/UpdateInfo`
						// 				// })
						// 			}
						// 		});

						// 		// uni.navigateTo({
						// 		// 	url: `/pages/signFor/signFor?Id=${'1774667297009438720'}`
						// 		// })
						// 		// this.AuthRealName()
						// 	}

						// 	// console.log('获取结果', result.Data)
						// 	// if (result.Data.State == true) {
						// 	// 	this.isCode = false
						// 	// 	this.status = true
						// 	// 	// 说明已经有账户信息 不用发送验证码
						// 	// 	uni.setStorageSync('userInfo', result.Data.userdata)
						// 	// 	this.userInfo = result.Data.userdata
						// 	// 	this.AuthRealName()
						// 	// } else {
						// 	// 	this.disabled = false
						// 	// 	this.status = false
						// 	// 	this.isCode = true

						// 	// 	this.getPhoneNumberTwo()
						// 	// }

						// }
					})
				// 以下代码先不删除后续需要打开
				// if (e.detail.errMsg.indexOf('fail') != -1) return
				// let pc = new WXBizDataCrypt('wxa24101ce1f340e47', this.session_key);
				// let data = pc.decryptData(e.detail.encryptedData, e.detail.iv);
				// uni.setStorageSync('userInfo', {
				// 	phone: data.phoneNumber
				// })
				// uni.redirectTo({
				// 	url: '/pagesA/index/index?q=' + this.query
				// })
			},
			getPhoneNumberTwo() {
				if (this.phone === "") {
					uni.showToast({
						title: '请先填写手机号',
						icon: "none"
					})
					return
				}
				if (!regPhone.test(this.phone)) {
					uni.showToast({
						title: '手机号格式不正确',
						icon: "none"
					});
					return;
				}
				if (this.agree == false) {
					uni.showToast({
						title: '请先阅读并同意协议',
						icon: "none"
					})
					return
				}
				if (this.code === "") {
					uni.showToast({
						title: '请输入验证码',
						icon: "none"
					})
					return
				}
				this.disabled = true
				this.$http('post', '/Base_Manage/Base_User/SaveWeChatUser', {
					"UserPhone": this.phone,
					"SmsCode": this.code
				}).then(res => {
					if (res.Success) {
						console.log('获取输入code信息结果', res.Data)
						console.log(res)
						this.userInfo = res.Data.userdata
						uni.setStorageSync('userInfo', this.userInfo)
						// 说明已经有账户信息 不用发送验证码
						this.AuthRealName()

					} else {
						uni.showToast({
							title: res.Msg,
							duration: 2000
						})
					}
				})
			}
		}
	}
</script>

<style scoped>
	.margin90 {
		margin-top: 90rpx;
	}

	.inputBorder {
		height: 40px;
		border-top: 1px solid #ccc;
		border-bottom: 1px solid #ccc;
	}

	/* 	::v-deep.uni-easyinput__content{
		border: 1px solid blue ;
	} */
	.auth {
		text-align: center;
		padding: 40rpx;
		padding-top: 80rpx
	}

	.image {
		width: 250rpx;
		height: 210rpx;
	}

	.apply {
		margin-top: 40rpx;
	}

	.description {
		margin: 40rpx 0;
		color: #999;
	}

	.flexBox {
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}

	.unit {
		position: fixed;
		bottom: 50px;
		text-align: center;
		right: 20rpx;
		left: 20rpx;
		color: #888
	}
</style>